<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>辅助功能维护界面</title>
    <script src="../../res/layui/layui.js"></script>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" type="text/css">
    <script src="../../res/js/Tools.js" charset="utf-8"></script>
    <script src="../../res/js/jquery-3.1.1.min.js" charset="utf-8"></script>
    <style>
        #site{
            font: bold 10px "微软雅黑";
        }
        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }
        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }
        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }
    </style>
    <script>
        //全局同步
        $.ajaxSetup({
            async:false
        });
    </script>
</head>
<body>
<form class="layui-form" action="" lay-filter="example" id="newsForm">
    <input type="hidden" id="attachId" name="attachId">
    <input type="hidden" id="attachType" name="attachType">
    <input type="hidden" id="auditState" name="auditState">
    <div class="layui-form-item">
        <label class="layui-form-label">标题：</label>
        <div class="layui-input-block">
            <input type="text" name="attachName" id="attachName"
                   placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text" id="newtext">
        <label class="layui-form-label">文本域：</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" id="attachContent" name="attachContent"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text" id="imgContent">
        <label class="layui-form-label">附件1：</label>
        <div class="layui-input-block">
            <input type="text" disabled="disabled" class="layui-input" id="site1" name="site1">
            <a href="javascript:;" class="file" id="file1">选择文件
                <input type="file" name="imgText1" id="imgText1">
            </a>
        </div>
        <div class="layui-form-item layui-form-text" id="imgContent2">
            <label class="layui-form-label">附件2：</label>
            <div class="layui-input-block">
                <input type="text" disabled="disabled" class="layui-input" id="site2" name="site2">
                <a href="javascript:;" class="file" id="file2">选择文件
                    <input type="file" name="imgText2" id="imgText2">
                </a>
            </div>
            <div class="layui-form-item layui-form-text" id="imgContent3">
                <label class="layui-form-label">附件3：</label>
                <div class="layui-input-block">
                    <input type="text" disabled="disabled" class="layui-input" id="site3" name="site3">
                    <a href="javascript:;" class="file" id="file3">选择文件
                        <input type="file" name="imgText3" id="imgText3">
                    </a>
                </div>
            </div>

            <div class="layui-form-item" id="watch">
                <label class="layui-form-label">可观看：</label>
                <div  class="layui-input-block" id="userLook">

                </div>
            </div>
            <div class="layui-form-item" id="submiting">
                <div class="layui-input-block">
                    <input type="button" class="layui-btn" value="立即提交" id="submit">
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
</form>
<script>
    $(function(){
        formreload();//修改与增加
        ycUser();//隐藏自身人员复选框
        fromsubmit();//表单提交
    })
    //选择显示文件名的方法
    function filename(){
        $("#file1").on("change","input[type='file']",function(){
            var filePath=$(this).val();
            var arr=filePath.split('\\');
            var fileName=arr[arr.length-1];
            $("#site1").val(fileName);
        })
        $("#file3").on("change","input[type='file']",function(){
            var filePath=$(this).val();
            var arr=filePath.split('\\');
            var fileName=arr[arr.length-1];
            $("#site3").val(fileName);
        })
        $("#file2").on("change","input[type='file']",function(){
            var filePath=$(this).val();
            var arr=filePath.split('\\');
            var fileName=arr[arr.length-1];
            $("#site2").val(fileName);
        })
    }
    //表单赋值及页面选择的方法
    function formreload(){
        var attachId=getQueryString("attachId");//获取传过来的ID值
        var attachType=getQueryString("attachType");//获取传过来的类型
        userselect();//显示需要选择的人员
        filename();//显示文件名
        if(attachId!=null){//不为空的话执行修改
            var url="../../AtsAttachmentAction/selectByPrimaryKey?attachId="+attachId;
            $.post(url,null,function(obj){
                $("#attachId").val(obj.attachId);
                $("#attachType").val(obj.attachType);
                $("#attachName").val(obj.attachName);
                $("#auditState").val(obj.auditState);
                $("#attachContent").val(obj.attachContent);
                $("#site1").val(obj.file1);
                $("#site2").val(obj.file2);
                $("#site3").val(obj.file3);
            })
        }else{//新增
            $("#attachType").val(attachType);
        }
        if(getQueryString("auditState")!=null){//如果是当前审核过来的话就隐藏提交框
            $("#submiting").hide();
        }
        if(getQueryString("jool")!=null){//如果是当前不审核过来的话就显示提交框
            if(getQueryString("auditState")==25&&getQueryString("jool")!='admin') {
                $("#submiting").show();
            }
        }
    }
    //显示人员
    function userselect(){
        var url="../../AtsAttachmentAction/userAll";
        $.post(url,null,function(obj){
            $.each(obj,function(index,item){
                $("#userLook").append("<div id='"+item.userId+"' class='layui-inline'><input type='checkbox' name='userId' value='"+item.userId+"."+item.userName+"' title='"+item.userName+"'></div>");
            })
        },'json')
        //给复选框设定的哪个选中
        if(getQueryString("attachId")!=null){//iD不为空则为修改
            var urls="../../AtsAttachmentEmpAction/atsfindId?attachId="+getQueryString("attachId");
            $.post(urls,null,function(obj){
                var look="";
                $.each(obj,function(index,item){
                    look+=item.userId+"."+item.userName;
                })
                $("input[name='userId']").each(function(indexs,items){
                    if(look.indexOf($(this).val())!=-1){
                        $(this).attr('checked',true);
                    }else{
                        $(this).attr('checked',false);
                    }
                });
            },'json')
        }
    }
    function ycUser(){
        var jool="";
        var urlk="../../AtsAttachmentEmpAction/findUser";
        $.post(urlk,null,function(obj){
            jool=obj.userId;
        },'json')
        $("input[name='userId']").each(function(indexs,items){
            if($(this).val().indexOf(jool)!=-1) {//找到自身的人员
                $("#"+jool).hide();//隐藏
                $(this).attr('checked',true);//选中
            }
        })
    }
    //表单提交的方法
    function fromsubmit() {
        layui.use(['form', 'layedit', 'laydate','upload'], function(){
            var layer=layui.layer;
            $("#submit").click(function () {
                var formData = new FormData(document.getElementById("newsForm"));
                var url = "../../AtsAttachmentAction/addUpdate";
                $.ajax({
                    url: url,
                    type: "post", // 传输类型
                    data: formData, // 数据
                    async: false, // 全局同步
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function (obj) {
                        if (obj.num == 1) {
                            layer.msg(obj.msg, {
                                icon: 1,
                                time: 1000,
                            }, function () {
                                parent.table2.reload();//刷新父页面
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;
                        }else if(obj.num==10){
                            layer.msg(obj.msg, {
                                icon: 1,
                                time: 1000,
                            }, function () {
                                parent.location.reload();//刷新父页面
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;
                        }
                    },
                    error: function (e) {
                        layer.alert("文件超出最大限额,请重新选择！");
                    }
                });
                return false
            });
        })
    }
</script>

</body>
</html>